package ee.serge.server.template;

import ee.serge.shared.Feedback;
import ee.serge.shared.Loan;


public class LoanDetailsTemplate extends Template {
   
   public LoanDetailsTemplate(String query) throws Exception {
      super(query);
   }

   private Loan loan;
   
   @Override
   protected void init() throws Exception {
      long id = Long.valueOf(paramMap.get("id"));
      loan = publicServiceImpl.getLoan(id);      
   }

   @Override
   protected String getTitle() {
      return loan.getName();
   }

   @Override
   protected String getHeader() {
      return loan.getName();
   }

   @Override
   protected String getHeader2() {
      return "";
   }

   @Override
   protected String getContent() {
      StringBuffer buffer = new StringBuffer();
      buffer.append("<table>");
      buffer.append(getRow("Link", loan.getLink()));
      buffer.append(getRow("APR", String.valueOf(loan.getAPR()) + "%"));
      buffer.append(getRow("Transfer fee", "£" + loan.getTransferFee()));
      buffer.append(getRow("Longest period", loan.getPeriod() + " days"));
      buffer.append(getRow("Minimum loan", "£" + loan.getMinLoan()));
      buffer.append(getRow("Maximum loan", "£" + loan.getMaxLoan()));
      buffer.append("</table>");
      
      buffer.append("<h2>Customer Reviews</h2>");
      buffer.append("<table>");
      buffer.append("<tr><th>#</th><th>Rating</th><th>Review</th></tr>");
      for (int i = 0; i < loan.getFeedbackList().size(); i++) {
         Feedback feedback = loan.getFeedbackList().get(i);
         buffer.append(getRow(String.valueOf(i), String.valueOf(feedback.getRating()), feedback.getComment()));
      }
      buffer.append("</table>");
      buffer.append("<a href='#'>Write your own review</a>");
      return buffer.toString();
   }

   private String getRow(String firstColumn, String secondColumn) {
      return "<tr><td>" + firstColumn + "</td><td>" + secondColumn + "</td></tr>";
   }
   
   private String getRow(String firstColumn, String secondColumn, String thirdColumn) {
      return "<tr><td>" + firstColumn + "</td><td>" + secondColumn + "</td><td>" + thirdColumn + "</td></tr>";
   }

}
